package com.fzhucc.utils;

import com.fzhucc.modules.security.config.WxProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * @author Woolsen
 * @since 2022/12/7 11:32
 */
@RequiredArgsConstructor
@Component
public class WxClient {

    private final WxProperties wxProperties;

    /**
     * 认证微信小程序code
     *
     * @return UnionId
     */
    public String authMpCode(String code) {
        return WxUtils.jscode2session(
                wxProperties.getMp().getAppId(),
                wxProperties.getMp().getAppSecret(),
                code
        ).unionId;
    }

    /**
     * 认证H5 code
     *
     * @return UnionId
     */
    public String authH5Code(String code) {
        return WxUtils.jscode2session(
                wxProperties.getH5().getAppId(),
                wxProperties.getH5().getAppSecret(),
                code
        ).unionId;
    }

    /**
     * 生成小程序二维码
     */
    public String generateMpQr(String path, String query) {
        return WxUtils.generateMpQr(
                wxProperties.getMp().getAppId(),
                wxProperties.getMp().getAppSecret(),
                path,
                query,
                wxProperties.getEnv()
        );
    }

    public String generateUrlLink(String path, String query, Long expireInMill) {
        return WxUtils.generateUrlLink(
                wxProperties.getMp().getAppId(),
                wxProperties.getMp().getAppSecret(),
                path,
                query,
                expireInMill,
                wxProperties.getEnv()
        );
    }

    public String generateMpQrWithNoParm(String path) {
        return WxUtils.generateMpQrWithNoParm(
                wxProperties.getMp().getAppId(),
                wxProperties.getMp().getAppSecret(),
                path
        );
    }

    public String generateScheme(String path, String query, Long expireInMill) {
        return WxUtils.generateScheme(
                wxProperties.getMp().getAppId(),
                wxProperties.getMp().getAppSecret(),
                path,
                query,
                expireInMill,
                wxProperties.getEnv()
        );
    }


}
